package main

import (
	"flag"
	"net/rpc"
	"neecola.com/learngo/zhenai/fetchserver"
	"log"
	"net"
	"net/rpc/jsonrpc"
)

// 使用命令行输入参数
var lsPort = flag.String("port", ":8888", "type in the server listen port")

func main() {
	flag.Parse()

	err := rpc.Register(&fetchserver.Fetch{})

	if err != nil {
		log.Fatalf("register rpc server %v  error %s ", fetchserver.Fetch{}, err)
	}

	listen, err := net.Listen("tcp", *lsPort)

	if err != nil {
		log.Fatalf("listen %s but error %s \n", *lsPort, err)
	}

	log.Printf("start rpc server and port is %s ", *lsPort)

	// 启动并且监听服务
	for {
		conn, err := listen.Accept()

		if err != nil {
			log.Printf("listen.Accept error %v \n", err)
			continue
		}

		go jsonrpc.ServeConn(conn)
	}

}
